package com.trendit.classicpos.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.trendit.classicpos.ClassicPosApp;
import com.trendit.classicpos.PosNative;
import com.trendit.classicpos.util.BitmapUtils;
import com.trendit.classicpos.util.Log;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

/**
 * 虚拟传统POS屏幕自定义控件
 *
 * @author zzx
 */
public class VirtualPosScreenView extends View {

    private Canvas canvasCache;
    private Bitmap bitmapCache;
    private Paint mPaint;
    private int scaleSize = 0;

    public VirtualPosScreenView(Context context, int scaleSize, int width, int height) {
        super(context);
        this.scaleSize = scaleSize;
        bitmapCache = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvasCache = new Canvas(bitmapCache);
        mPaint = new TextPaint();
        Typeface typeface = Typeface.createFromAsset(ClassicPosApp.getInstance().getAssets(), "fonts/monospace.ttf");
        mPaint.setTypeface(typeface);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(0);
        mPaint.setTextSize(24);
    }

    public VirtualPosScreenView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    public void clearRect(int left, int top, int right, int bottom) {
        Paint paint = new Paint(ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        canvasCache.drawRect(left * this.scaleSize, (top + 1) * this.scaleSize, right * this.scaleSize, (bottom + 2) * this.scaleSize, paint);
    }

    private int rgbToColor(int rgb565) {
        return Color.rgb(((rgb565 >> 11) & 0xff) << 3, ((rgb565 >> 5) & 0x3f) << 2, (rgb565 & 0x1f) << 3);
    }

    public void fillRect(int left, int top, int right, int bottom, int rgb565) {
        Paint paint = new Paint(ANTI_ALIAS_FLAG);
        paint.setColor(rgbToColor(rgb565));
        canvasCache.drawRect(left * this.scaleSize, (top + 1) * this.scaleSize, right * this.scaleSize, (bottom + 2) * this.scaleSize, paint);
    }

    /**
     * 显示文本，因为屏幕分辨率扩大了，所以x 和y要*2，y轴的计算方式不一样，所以需要加上字体大小
     *
     * @param x    x坐标
     * @param y    y坐标
     * @param text 显示文本
     */
    public void showText(int x, int y, String text) {
        canvasCache.drawText(text, x * this.scaleSize, y * this.scaleSize, mPaint);
    }

    private static byte[] transBitmap(byte[] src) {
        byte[] dest = new byte[src.length];
        for (int i = 0; i < src.length; i = i + 2) {
            dest[i] = src[i + 1];
            dest[i + 1] = src[i];
        }
        return dest;
    }

    public void showBitmap(int left, int top, int width, int height, byte[] pic) {
        Bitmap bitmap = BitmapUtils.getOriginalBitmap(transBitmap(pic), width, height, Bitmap.Config.RGB_565);
        Bitmap saleBitmap = BitmapUtils.scaleBitmap(bitmap, this.scaleSize);
        canvasCache.drawBitmap(saleBitmap, left * this.scaleSize, top * this.scaleSize, new Paint(ANTI_ALIAS_FLAG));
    }

    public void showMonochromeBitmap(int left, int top, int width, int height, byte[] pic) {
        Bitmap bitmap = BitmapUtils.buildMonoChromeBitmap(width, height, pic);
        Bitmap saleBitmap = BitmapUtils.scaleBitmap(bitmap, this.scaleSize);
        canvasCache.drawBitmap(saleBitmap, left * this.scaleSize, top * this.scaleSize, new Paint(ANTI_ALIAS_FLAG));
    }

    public void refreshScreen() {
        this.postInvalidate();
    }


    public void setTextColor(int rgb565TextColor) {
        mPaint.setColor(rgbToColor(rgb565TextColor));
    }

    public void setTextFont(int fontSize) {
        mPaint.setTextSize(fontSize * 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmapCache, 0, 0, mPaint);
    }
}
